package com.mdroid.support.widgets.text;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.style.DynamicDrawableSpan;

/**
 * 将String转换成Bitmap
 * @author William.cheng
 * @version 创建时间：2011-12-26 上午10:30:22
 */
public class TextSpan extends DynamicDrawableSpan {
    private String mText;
    private int mTextColor;

    public TextSpan(String text) {
        this(text, 0, 0);
    }
    
    public TextSpan(String text, int textColor) {
        this(text, textColor, ALIGN_BOTTOM);
    }

    public TextSpan(String text, int textColor, int verticalAlignment) {
        super(verticalAlignment);
        this.mText = text;
        this.mTextColor = textColor == 0 ? Color.BLACK : textColor;
    }

    //这里这个没用。不会被调用
    @Override
    public Drawable getDrawable() {
        Drawable drawable = null;
        
        return drawable;
    }
    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end,
            float x, int top, int y, int bottom, Paint paint) {
        paint.setColor(mTextColor);
        canvas.save();
        canvas.drawText(mText, x, y, paint);
        canvas.restore();
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end,
            FontMetricsInt fm) {
        Rect rect = new Rect();
        paint.getTextBounds(mText, 0, mText.length(), rect);
        
        if (fm != null) {
            fm.top = -rect.height();
            fm.descent = 5;
            
            fm.ascent = rect.top;
            fm.bottom = fm.descent;
        }
        return rect.right + 5;
    }

}
